package weaver.interfaces.cese.duss.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.axis.encoding.Base64;
import weaver.docs.webservices.DocAttachment;
import weaver.docs.webservices.DocInfo;
import weaver.docs.webservices.DocServiceImpl;
import weaver.general.BaseBean;
import weaver.hrm.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-02-23
 * @Description: 上传文件接口
 * @ModifyLog:
 **/
@WebServlet("/upload/uploadFile")
public class UploadFileServiceImpl extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.doPost( req, res );
    }
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        int userid = 1;//创建人
        String rawFileName = req.getParameter("filename");//文件名称
        PrintWriter out = res.getWriter();
            byte[] content = new byte[102400];
            // 上传附件，创建html文档
            content = null;
            try {
                int byteread;
                byte data[] = new byte[1024];
                InputStream input = req.getInputStream();
                String str = input.toString();
                new BaseBean().writeLog(str);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                while ((byteread = input.read(data)) != -1) {
                    outputStream.write(data, 0, byteread);
                    outputStream.flush();
                }
                content = outputStream.toByteArray();
                input.close();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            int docid = -1;
            DocServiceImpl docserviceimpl = new DocServiceImpl();
            DocAttachment[] docattachment = new DocAttachment[1];
            String docsubject = ""; //文档标题
            String keyword = ""; //文档内容
            String filetype = "";
            String filename = "";
            if (rawFileName.lastIndexOf( "." ) > -1) {
                filetype = rawFileName.substring( rawFileName.lastIndexOf( "." ) + 1 );
                filename = rawFileName.substring( 0, rawFileName.lastIndexOf( "." ) );
            }
            try {
                DocAttachment da = new DocAttachment();
                da.setFilename( rawFileName);//附件标题
                da.setFilecontent( Base64.encode(content)); //附件内容
                da.setFiletype( filetype ); //附件类型
                da.setImagefilename( rawFileName);// 附件标题
                da.setIszip( 1 );//是否压缩
                da.setIsAesEncrype( 0 );//是否加密
                da.setAesCode( "0" );
                da.setIsextfile( "1" ); //附件是否是office类型
                if (filetype.equalsIgnoreCase( "doc" )) {
                    da.setDocfiletype( "3" );
                } else if (filetype.equalsIgnoreCase( "xls" )) {
                    da.setDocfiletype( "4" );
                } else if (filetype.equalsIgnoreCase( "ppt" )) {
                    da.setDocfiletype( "5" );
                } else if (filetype.equalsIgnoreCase( "wps" )) {
                    da.setDocfiletype( "6" );
                } else if (filetype.equalsIgnoreCase( "docx" )) {
                    da.setDocfiletype( "7" );
                } else if (filetype.equalsIgnoreCase( "xlsx" )) {
                    da.setDocfiletype( "8" );
                } else if (filetype.equalsIgnoreCase( "pptx" )) {
                    da.setDocfiletype( "9" );
                } else {
                    da.setDocfiletype( "2" );
                }
                docattachment[0] = da;
                docsubject = filename;
                keyword = filename;
                DocInfo doc = new DocInfo();//创建文档
                doc.setMaincategory( 29041 );//主目录id
                //doc.setSubcategory( 29041 );//分目录id
                doc.setSeccategory( 29042 );//子目录id
                doc.setDocSubject( docsubject );//标题
                doc.setDoccontent( "" );//文档内容
                doc.setKeyword( keyword );
                doc.setAttachments( docattachment );
                if (!"".equals( userid )) {
                    User user = new User( userid );
                    docid = docserviceimpl.createDocByUser( doc, user );
                }
                JSONObject resultJson = new JSONObject(  );
                resultJson.put("docid",docid);
                out.print(resultJson.toJSONString());
            } catch (Exception e) {
                e.printStackTrace();
                out.print("request error");
            }

    }
}
